package com.unionfetch.content_home.model

import com.google.gson.Gson
import com.google.gson.JsonObject
import com.unionfetch.content_home.api.HomeApiManager
import com.unionfetch.content_home.bean.SumsubAccessToken
import com.unionfetch.content_home.bean.request.UploadKYCFile
import com.unionfetch.content_home.contract.KYCContract
import com.unionftech.common.base.mvp.impl.BaseModel
import com.unionftech.common.base.mvp.impl.IBaseCallBack
import com.unionftech.common.bean.ResponseResult
import com.unionftech.common.network.SimpleNetWork
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONArray

class KYCAuthModel : BaseModel(), KYCContract.IKYCModel {
    override fun resetSumsub(
        token: String, userId: String, callBack: IBaseCallBack<ResponseResult<Any?>>
    ) {
        execute {
            val responseResult = request {
                val jsonObject = JsonObject()
                jsonObject.addProperty("userId", userId)
                val json = jsonObject.toString()
                HomeApiManager.getInstance().apiKYC().resetSumsub(
                    token, json.toRequestBody(SimpleNetWork.JSON)
                )
            }
            callBack.onResponse(responseResult)
        }
    }

    override fun resetSumsubPoa(
        token: String, userId: String, callBack: IBaseCallBack<ResponseResult<Any?>>
    ) {
        execute {
            val responseResult = request {
                val jsonObject = JsonObject()
                jsonObject.addProperty("userId", userId)
                val json = jsonObject.toString()
                HomeApiManager.getInstance().apiKYC()
                    .resetSumsubPoa(token, json.toRequestBody(SimpleNetWork.JSON))
            }
            callBack.onResponse(responseResult)
        }
    }

    override fun getSumsubPoiToken(
        token: String, userId: String, callBack: IBaseCallBack<ResponseResult<SumsubAccessToken>>
    ) {
        execute {
            val responseResult = request {
                HomeApiManager.getInstance().apiKYC().getSumsubPoiToken(token, userId)
            }
            callBack.onResponse(responseResult)
        }
    }

    override fun getSumsubPoaToken(
        token: String, userId: String, callBack: IBaseCallBack<ResponseResult<SumsubAccessToken>>
    ) {
        execute {
            val responseResult = request {
                HomeApiManager.getInstance().apiKYC().getSumsubPoaToken(token, userId)
            }
            callBack.onResponse(responseResult)
        }
    }

    override fun notifyPoiResult(
        token: String,
        userId: String,
        isSupplement: Boolean,
        callBack: IBaseCallBack<ResponseResult<Any?>>
    ) {
        execute {
            val responseResult = request {
                val jsonObject = JsonObject()
                jsonObject.addProperty("userId", userId)
                jsonObject.addProperty("isSupplement", isSupplement)
                val json = jsonObject.toString()
                HomeApiManager.getInstance().apiKYC()
                    .notifyPoiResult(token, json.toRequestBody(SimpleNetWork.JSON))
            }
            callBack.onResponse(responseResult)
        }
    }

    override fun notifyPoaResult(
        token: String,
        userId: String,
        isSupplement: Boolean,
        callBack: IBaseCallBack<ResponseResult<Any?>>
    ) {
        execute {
            val responseResult = request {
                val jsonObject = JsonObject()
                jsonObject.addProperty("userId", userId)
                jsonObject.addProperty("isSupplement", isSupplement)
                val json = jsonObject.toString()
                HomeApiManager.getInstance().apiKYC()
                    .notifyPoaResult(token, json.toRequestBody(SimpleNetWork.JSON))
            }
            callBack.onResponse(responseResult)
        }
    }

    override fun uploadPoi(
        token: String,
        kycFile: UploadKYCFile,
        isSupplement: Boolean,
        callBack: IBaseCallBack<ResponseResult<Any?>>
    ) {
        execute {
            val responseResult = request {
                val json = Gson().toJson(kycFile, UploadKYCFile::class.java)
                if (isSupplement) {
                    HomeApiManager.getInstance().apiKYC()
                        .supplementPoi(token, json.toRequestBody(SimpleNetWork.JSON))
                } else {
                    HomeApiManager.getInstance().apiKYC()
                        .uploadPoi(token, json.toRequestBody(SimpleNetWork.JSON))
                }
            }
            callBack.onResponse(responseResult)
        }
    }

    override fun uploadPoa(
        token: String,
        kycFile: UploadKYCFile,
        isSupplement: Boolean,
        callBack: IBaseCallBack<ResponseResult<Any?>>
    ) {
        execute {
            val responseResult = request {
                val json = Gson().toJson(kycFile, UploadKYCFile::class.java)
                if (isSupplement) {
                    HomeApiManager.getInstance().apiKYC()
                        .supplementPoa(token, json.toRequestBody(SimpleNetWork.JSON))
                } else {
                    HomeApiManager.getInstance().apiKYC()
                        .uploadPoa(token, json.toRequestBody(SimpleNetWork.JSON))
                }
            }
            callBack.onResponse(responseResult)
        }
    }
}